package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.bean.LibraryBean1;
import com.wnhz.work.yz.bean.LibraryBean2;
import com.wnhz.work.yz.fragment.FragmentLibrary;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 文库
 */
public class LibraryActivity extends BaseActivity implements TabLayout.OnTabSelectedListener, View.OnClickListener {

    private TabLayout tabLayout;

    private List<BaseFragment> list_fragment = new ArrayList<>(); //定义要装fragment的列表
    private List<String> list_title = new ArrayList<>(); //tab名称列表
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
//    private int[] titles = new int[]{R.string.library_tab1, R.string.library_tab2, R.string.library_tab3, R.string.library_tab4};

    private List<LibraryBean1> beanList = new ArrayList<>();
    private List<LibraryBean2> libraryList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library);
        initTitle();
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);


//        for (int i = 0; i < titles.length; i++) {
//            list_fragment.add(FragmentLibrary.newInstance());
//            tabLayout.addTab(tabLayout.newTab().setText(titles[i]).setTag(i));
//            list_fragment.get(i).setType(i);
//        }

        uoload();
    }

    private static final String TAG = "LibraryActivity";

    private void uoload() {
        showDialog();
        XUtil.Post(Url.WENKU_GETCATELIST, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: wenku=  " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            LibraryBean1 bean = new LibraryBean1();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), LibraryBean1.class);
                            beanList.add(bean);
                        }
                    }

                    list_title.clear();
                    list_fragment.clear();
                    for (int i = 0; i < beanList.size(); i++) {
                        list_fragment.add(FragmentLibrary.newInstance());
                        tabLayout.addTab(tabLayout.newTab().setText(beanList.get(i).getTitle()).setTag(i));
                        list_fragment.get(i).setType(i);
                        libraryList = beanList.get(i).getInner();
                        FragmentLibrary fragment = (FragmentLibrary) list_fragment.get(i);
                        fragment.setData(libraryList);

                    }
                    Log.i(TAG, "onSuccess: bean2=  " + libraryList.size());

                    for (LibraryBean2 bean2 : libraryList) {
                        Log.i(TAG, "onSuccess: bean2=  " + bean2.toString());
                    }

                    tabLayout.setOnTabSelectedListener(LibraryActivity.this);

                    fragmentManager = getSupportFragmentManager();
                    transaction = fragmentManager.beginTransaction();
                    for (int i = 0; i < list_fragment.size(); i++) {
                        transaction.add(R.id.fl_all_order, list_fragment.get(i));
                        if (i != 0) {
                            transaction.hide(list_fragment.get(i));
                        }
                    }
                    transaction.commit();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < list_fragment.size(); i++) {
            if (i == index) {
                transaction.show(list_fragment.get(i));
            } else {
                transaction.hide(list_fragment.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int clickTab = (int) tab.getTag();
        hideFragment(clickTab);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.library_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        RelativeLayout btn_main_right = (RelativeLayout) findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
